/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pima.gestrest.controller;

import com.pima.gestrest.business.VilleEJB;
import com.pima.gestrest.model.Ville;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.ejb.EJB;
import java.util.Iterator;
import java.util.List;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
/**
 *
 * @author Mat
 */

@ManagedBean
@SessionScoped
public class VilleController {

    // ======================================
    // =             Attributes             =
    // ======================================

    @EJB
    private VilleEJB villeEJB;
    private HtmlDataTable dataTable;
    private Ville ville = new Ville();
    private ListDataModel villeList;
    private ArrayList<SelectItem> itemList;

    private void updateItemList() {
        itemList = new ArrayList<SelectItem>(villeEJB.getItems());
    }

    private void updateVilleList() {
        villeList = new ListDataModel(villeEJB.findAll());
    }

    // ======================================
    // =           Public Methods           =
    // ======================================

    public String doNew() {
        ville = new Ville();
        return "../secured/newVille.faces";
    }

    public String doCreate() {
        villeEJB.create(ville);
        return "../resources/listVilles.faces";
    }

    public String doDelete() {
        List<Ville> villes = (List<Ville>)villeList.getWrappedData();
        villeEJB.delete(onlySelected(villes));
        return "../resources/listVilles.xhtml";
    }

    private List<Ville> onlySelected(List<Ville> list) {
        for (Iterator<Ville> it = list.iterator(); it.hasNext(); ) {
            if (!(it.next().isSelected()))
                it.remove();
        }
        return list;
    }

    public String viewDetail () {
        ville = (Ville)villeList.getRowData();
        return "../resources/viewVille.faces";
    }

    public String doEdit() {
        //ville = (Ville)villeList.getRowData();
        return "../secured/editVille.xhtml";
    }

    public String doSave() {
        villeEJB.update(ville);
        return "../resources/listVilles.xhtml";
    }
    // ======================================
    // =          Getters & Setters         =
    // ======================================

    public Ville getVille() {
        return ville;
    }

    public void setVille(Ville ville) {
        this.ville = ville;
    }

    public ListDataModel getVilleList() {
        updateVilleList();
        return villeList;
    }

    public void setVilleList(ListDataModel villeList) {
        this.villeList = villeList;
    }

    public HtmlDataTable getDataTable() {
        return dataTable;
    }

    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }

    public List<SelectItem> getItemList() {
        updateItemList();
        return itemList;
    }

    public void setItemList(ArrayList<SelectItem> list) {
        itemList = list;
    }


}